package control;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import util.HibernateUtil;

public class ConnectHandler {
    
    private Session session;
    
     
    
    public ConnectHandler() throws FileNotFoundException, IOException {
         session = GetSessionFactory().openSession();
    
    }
    
    
    public Session getSession(){
        return session;
    }
    
     public static SessionFactory GetSessionFactory() throws FileNotFoundException, IOException {
        Properties prop = new Properties();

        prop.load(new FileInputStream("database.properties"));

        AnnotationConfiguration conf = new AnnotationConfiguration().configure();
        // <!-- Database connection settings -->

        conf.setProperty("hibernate.connection.url", "jdbc:mysql://"+prop.getProperty("ip")+ ":"+prop.getProperty("port")+ "/"+prop.getProperty("database"));
        conf.setProperty("hibernate.connection.username", prop.getProperty("brugernavn"));
        conf.setProperty("hibernate.connection.password", prop.getProperty("kodeord"));
       SessionFactory SESSION_FACTORY = conf.buildSessionFactory();



        return SESSION_FACTORY;
    }
 
    
    
}
